/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tests.taskgraph.tasks.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.TreeSet;

import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import taskgraph.channels.Channel;
import taskgraph.ports.InputPort;
import taskgraph.ports.OutputPort;
import taskgraph.tasks.data.WriteCollection;

public class WriteCollectionTest {

	/**
	 * Runs the test suite in this class from the command line.
	 * 
	 * @param args	Arguments are ignored.
	 */
	public static void main(String[] args) {
		org.junit.runner.JUnitCore.runClasses(WriteCollectionTest.class);
	}
	
	/**
	 * Provides compatibility with 3.x versions of JUnit.
	 * 
	 * @return A 3.x-compatible test suite.
	 */
	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(WriteCollectionTest.class);
	}
	
	private InputPort<Product> input;
	
	@Before
	public void setup() {
		Channel<Product> channel = new Channel<Product>();
		input = channel.getInputPort();
	}
	
	@After
	public void cleanup() throws IOException {
		input.close();
	}
	
    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the constructors.                                               | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void defaultConstructor() {
		WriteCollection<Product> task = new WriteCollection<Product>();
		Assert.assertNotNull(task);
	}
	
	@Test
	public void defaultInstanceClass() {
		WriteCollection<Product> task = new WriteCollection<Product>();
		Assert.assertEquals(WriteCollection.class, task.getClass());
	}

	@Test
	public void constructorWithPort() {
		Collection<Product> collection = Collections.emptyList();
		WriteCollection<Product> task = 
			new WriteCollection<Product>(input, collection);
		Assert.assertNotNull(task);
	}
	
	@Test
	public void constructorWithPortAndClass() {
		Collection<Product> collection = Collections.emptyList();
		WriteCollection<Product> task = 
			new WriteCollection<Product>(input, collection);
		Assert.assertEquals(WriteCollection.class, task.getClass());
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the run method.                                                 | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void testRunWithList() throws IOException {
		Product[] products = {
			new Product(5005, "Notebook", 2.50f),
			new Product(1400, "Pencil",   0.49f),
			new Product(0001, "Eraser",   0.60f),
			new Product(4040, "Bookend", 10.25f)
		};
		
		ArrayList<Product> list = new ArrayList<Product>();

		Channel<Product> channel = new Channel<Product>();
		WriteCollection<Product> task = 
			new WriteCollection<Product>(channel.getInputPort(), list);
		
		OutputPort<Product> port = channel.getOutputPort();
		for (Product p : products)
			port.write(p);
		port.close();
				
		task.run();

		Iterator<Product> itor = list.iterator();
		for (Product p : products)
			Assert.assertEquals(p,  itor.next());
	}

	@Test
	public void testRunWithSet() throws IOException {
		Product[] products = {
				new Product(0001, "Eraser",   0.60f),
				new Product(1400, "Pencil",   0.49f),
				new Product(4040, "Bookend", 10.25f),
				new Product(5005, "Notebook", 2.50f)
			};
			
			TreeSet<Product> tree = new TreeSet<Product>();

			Channel<Product> channel = new Channel<Product>();
			WriteCollection<Product> task = 
				new WriteCollection<Product>(channel.getInputPort(), tree);
			
			OutputPort<Product> port = channel.getOutputPort();
			for (Product p : products)
				port.write(p);
			port.close();
					
			task.run();

			Iterator<Product> itor = tree.iterator();
			for (Product p : products)
				Assert.assertEquals(p,  itor.next());
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests for constructors.                                     | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullInputPort() throws IOException {
		Collection<Product> collection = Collections.emptyList();
		new WriteCollection<Product>(null, collection);
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullCollection() throws IOException {
		new WriteCollection<Product>(input, null);
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests.                                                      | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

}
